<?php


function cartesianProduct(...$arrays)
{
    $result = [];

    // 递归函数，用于生成组合
    $rec = function ($arrays, $index, $current) use (&$result, &$rec) {
        if ($index < count($arrays)) {
            foreach ($arrays[$index] as $key => $item) {
                $rec($arrays, $index + 1, array_merge($current, [$key=>$item]));
            }
        } else {
            $result[] = $current;
        }
    };

    // 调用递归函数
    $rec($arrays, 0, []);

    return $result;
}

$array1 = [
    'a1' => 'Color: Red',
    'a2' => 'Color: Blue',
    'a3' => 'Color: Green',
];
$array2 = [
    'b1' => 'Size: S',
    'b2' => 'Size: M',
];

$array4 = [
    'c1' => 'bbb: b1',
    'c2' => 'bbb: b2',
    'c3' => 'bbb: b3',
];
// 使用递归函数生成组合
$array3 = cartesianProduct($array1, $array2, $array4);

foreach ($array3 as $key => $value) {
    echo json_encode($value) . PHP_EOL;
}
// 打印结果
